﻿using System;
using System.Windows.Forms;
using System.Xml;
using Library;
using Engine;

namespace GameTool
{
    public partial class LoginForm : Form
    {
        public MainForm mainForm;

        XmlDocument xDoc;
        XmlNodeList username;
        XmlNodeList password;
        XmlNodeList permission;

        public LoginForm()
        {
            try
            {
                InitializeComponent();
                DeleleFile();
                GetXML();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        private void LoginButton_Click(object sender, EventArgs e)
        {
            try
            {
                bool OK = false;
                int index = -1;
                for (int i = 0; i < username.Count; i++)
                {
                    if (this.txtUserName.Text == username[i].InnerText)
                    {
                        index = i;
                        User.UserName = this.txtUserName.Text;
                        break;
                    }
                }
                if (index >= 0) // Tim thay mot nguoi.
                {
                    if (this.txtPass.Text == password[index].InnerText)
                    {
                        OK = true;
                    }
                }
                if (OK)
                {
                    string per = permission[index].InnerText;
                    string[] pers = per.Split(' ');
                    for (int j = 0; j < pers.Length; j++)
                    {
                        User.permission = pers[j];
                    }
                    this.Hide();
                    this.mainForm.ShowDialog();
                }
                else
                {
                    MessageBox.Show("ĐĂNG NHẬP THẤT BẠI\n\n\nTên đăng nhập hoặc mật khẩu không đúng.\nXin bạn vui lòng đăng nhập lại!\n\n\n", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void DeleleFile()
        {
            try
            {
                string folder = Environment.CurrentDirectory;
                string[] files = System.IO.Directory.GetFiles(folder);
                for (int i = 0; i < files.Length; i++)
                {
                    if (files[i].Contains(".png"))
                    {
                        System.IO.File.Delete(files[i]);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        private void GetXML()
        {
            try
            {
                string _fileName = "user.xml";
                //ftp_main.download(Common.config.pathXml + "\\Per", _fileName);
                xDoc = new XmlDocument();
                xDoc.Load(_fileName);
                username = xDoc.GetElementsByTagName("username");
                password = xDoc.GetElementsByTagName("password");
                permission = xDoc.GetElementsByTagName("permission");
                //System.IO.File.Delete(_fileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }
    }
}
